package com.adams.core.exception;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * File Name : ExceptionConfigCache.java
 * 
 * @Description : 异常配置类
 * @author 904203 肖震
 */
public class ExceptionConfigCache
{
	private static Logger logger = LoggerFactory
			.getLogger(ExceptionConfigCache.class);

	private static ExceptionConfigCache instance;
	// 缓存的内容
	private static Map<String, Error> cache = new HashMap<String, Error>();
	// 错误消息配置文件名称
	private static final String ERROR_MESSAGE_FILE_NAME = "error_message.xml";

	// 私有化F
	private ExceptionConfigCache()
	{
		init();
	}

	public static ExceptionConfigCache getInstance()
	{
		if (instance == null)
		{
			synchronized (ExceptionConfigCache.class)
			{// double check
				if (instance == null)
				{
					instance = new ExceptionConfigCache();
				}
			}
		}
		return instance;
	}

	private void init()
	{
		SAXReader reader = new SAXReader();
		try
		{
			Document document = reader.read(ExceptionConfigCache.class
					.getClassLoader().getResourceAsStream(
							ERROR_MESSAGE_FILE_NAME));
			parse(document.getRootElement());
		} catch (DocumentException e)
		{
			logger.error("解析error_message.xml文件出现错误,cause:", e);
			// 打堆栈到控制台,防止logger没有被正确初始化
			e.printStackTrace();
		}
	}

	private void parse(Element rootElement)
	{
		if (rootElement == null)
		{
			throw new RuntimeException("error_message.xml配置文件错误");
		}
		List<DefaultElement> errors = rootElement.elements("error");
		if (errors != null && errors.size() > 0)
		{
			for (DefaultElement item : errors)
			{
				Error error = new Error();
				error.setCode(item.elementText("code"));
				if (error.getCode() == null
						|| error.getCode().trim().length() == 0)
				{
					throw new RuntimeException(
							"error_message.xml配置文件的配置信息错误,原因:存在code为空的配置项,请检查error_message.xml文件");
				}
				error.setText(item.elementText("text"));
				error.setDescription(item.elementText("description"));
				cache.put(error.getCode(), error);
			}
		}

	}

	/**
	 * 
	 * Description : 读描述信息
	 * 
	 * @param errorCode
	 * @return
	 * 
	 */
	public String getErrorDesc(String errorCode)
	{
		Error error = getError(errorCode);
		if (error != null)
		{
			return error.getDescription();
		}
		return null;

	}

	/**
	 * 
	 * Description : 读异常信息
	 * 
	 * @param errorCode
	 * @return
	 * 
	 */
	public String getErrorText(String errorCode)
	{
		Error error = getError(errorCode);
		if (error != null)
		{
			return error.getText();
		}
		return null;

	}

	/**
	 * 
	 * Description : 读异常配置信息
	 * 
	 * @param errorCode
	 * @return
	 * 
	 */
	public Error getError(String errorCode)
	{
		if (errorCode == null || errorCode.trim().length() == 0)
		{
			return null;
		}
		return cache.get(errorCode);
	}

	/**
	 * Description : <Detail Description for method>
	 * 
	 * @param args
	 * 
	 */

//	public static void main(String[] args)
//	{ // TODO Auto-generated method
//
//		System.out.println(ExceptionConfigCache.getInstance().getErrorText(
//				"template.name.repeat"));
//	}

}
